﻿Imports Windows.Phone.Speech.Synthesis
Imports Windows.Foundation
Imports System.Threading.Tasks

Namespace Utility
    Public Module SpeechHelper
        Dim task As IAsyncAction

        Public ReadOnly Property CanCancel As Boolean
            Get
                If task IsNot Nothing Then
                    Return task.Status = TaskStatus.Running
                End If
                Return False
            End Get
        End Property

        Public ReadOnly Property Voices As IReadOnlyList(Of VoiceInformation)
            Get
                Return InstalledVoices.All
            End Get
        End Property

        Public Sub Speak(txt As String, voice As VoiceInformation)
            Dim synth As New SpeechSynthesizer
            synth.SetVoice(voice)
            task = synth.SpeakTextAsync(txt)
        End Sub

        Public Sub Speak(txt As String)
            Speak(txt, InstalledVoices.Default)
        End Sub

        Public Sub [Stop]()
            If task IsNot Nothing Then
                task.Cancel()
            End If
        End Sub
    End Module
End Namespace